package com.be.beadmin.product.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.product.dto.input.ProductProcessBomInforDto;
import com.be.beadmin.product.dto.output.ProductProcessBomInforEx;
import com.be.beadmin.product.service.ProductProcessBomInforService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author zjm
* @date 2023-03-19
**/
@RestController
public class ProductProcessBomInforController {

    @Autowired
    private  ProductProcessBomInforService productProcessBomInforService;


    @PostMapping(value = "/product/getProductProcessBomInforByPage")
    public ResultModel getProductProcessBomInforByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(productProcessBomInforService.getProductProcessBomInforByPage(pageEntity)) ;
    }

    @PostMapping("/product/addProductProcessBomInfor")
    public ResultModel addProductProcessBomInfor(@RequestBody ProductProcessBomInforDto productProcessBomInforDto){
        productProcessBomInforService.addProductProcessBomInfor(productProcessBomInforDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/product/delProductProcessBomInfor")
    public ResultModel delProductProcessBomInfor(@RequestBody ProductProcessBomInforDto productProcessBomInforDto){
        productProcessBomInforService.delProductProcessBomInfor(productProcessBomInforDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/product/updateProductProcessBomInfor")
    public ResultModel updateProductProcessBomInfor(@RequestBody ProductProcessBomInforDto productProcessBomInforDto){
        productProcessBomInforService.updateProductProcessBomInfor(productProcessBomInforDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/product/exportProductProcessBomInfor")
    public void exportProductProcessBomInfor(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<ProductProcessBomInforEx> productProcessBomInforPage = productProcessBomInforService.getProductProcessBomInforByPage(pageEntity);
        List<ProductProcessBomInforEx> productProcessBomInforExes = productProcessBomInforPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("ProductProcessBomInfor接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ProductProcessBomInforEx.class).sheet("sheet1").doWrite(productProcessBomInforExes);
    }
}